<template>
  <DaprPlusTable
    :table-title="$t('fsb.games.gameMap.list')"
    :entity="entity"
    :grid-options="gridOptions"
    :query-api="crudApi.getPage"
    :create-api="crudApi.create"
    :update-api="crudApi.update"
    :delete-api="crudApi.delete"
    :default-new-record="createDefaultGameMap"
    @update:grid-api="onGridApiUpdate"
  />
</template>

<script lang="ts" setup>
import type { ExtendedVxeGridApi, VxeGridProps } from '#/adapter/vxe-table';
import { DaprPlusTable } from '#/components/daprplus';
import { createCrudApi } from '#/api/daprplus';
import type { IOutDto, IInDto, EntityInfo } from '#/utils/daprplus';
import { createBooleanColumn } from '#/utils/daprplus';
import { $t } from '#/locales';

const baseUrl = '/games/api/gamemap';
const crudApi = createCrudApi<GameMapInDto, GameMapOutDto>(baseUrl);

const entity: EntityInfo = {
  code: 'GameMap',
  display: $t('fsb.games.gameMap.title'),
};

// 验证规则
const validRules = {
  name: [
    { required: true, message: '地图名称不能为空' },
    { min: 2, max: 50, message: '地图名称长度应在2-50个字符之间' },
  ],
  transMapNames: [{ max: 500, message: '传送地图名称长度应在0-500个字符之间' }],
};

// 扩展配置
const gridOptions: VxeGridProps<GameMapOutDto> = {
  editRules: validRules,
  columns: [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      sortable: true,
      width: 130,
    },
    {
      field: 'name',
      title: '地图名称',
      sortable: true,
      align: 'left',
      minWidth: 150,
      editRender: { name: 'input' },
    },
    createBooleanColumn('isOutdoor', '是否户外', {
      sortable: true,
      width: 111,
    }),
    createBooleanColumn('canFight', '可否战斗', { width: 111 }),
    createBooleanColumn('hasTrans', '有传送点', { width: 111 }),
    {
      field: 'transMapNames',
      title: '传送地图',
      align: 'left',
      minWidth: 200,
      editRender: { name: 'input' },
    },
    {
      field: 'createdTime',
      title: '创建时间',
      formatter: 'formatDateTime',
      width: 150,
    },
  ],
};

let gridApi: ExtendedVxeGridApi;

function onGridApiUpdate(api: ExtendedVxeGridApi) {
  gridApi = api;
}

function createDefaultGameMap(): GameMapInDto {
  return {
    id: '0',
    name: '新地图',
    isOutdoor: false,
    canFight: false,
    hasTrans: false,
    transMapNames: '',
  };
}

export interface GameMapOutDto extends IOutDto {
  name?: string;
  isOutdoor?: boolean;
  canFight?: boolean;
  hasTrans?: boolean;
  transMapNames?: string;
  createdTime?: Date;
}

export interface GameMapInDto extends IInDto {
  name: string;
  isOutdoor: boolean;
  canFight: boolean;
  hasTrans: boolean;
  transMapNames: string;
}
</script>

<style scoped></style>
